package com.code2roc.jarmanage.job;

import com.code2roc.jarmanage.db.ProjectDB;
import com.code2roc.jarmanage.db.WarDB;
import com.code2roc.jarmanage.model.ProjectDO;
import com.code2roc.jarmanage.model.WarDO;
import com.code2roc.jarmanage.util.*;
import org.apache.commons.io.FileUtils;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.io.File;
import java.io.FilenameFilter;
import java.util.*;

@DisallowConcurrentExecution
public class LogFileRollJob implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        boolean logFileRoll = ConvertUtil.convert2Boolean(ConfigUtil.getConfigValue("logFileRoll"));
        int logFileRollSize = ConvertUtil.convert2Int(ConfigUtil.getConfigValue("logFileRollSize"));
        int logFilekeepFiles = ConvertUtil.convert2Int(ConfigUtil.getConfigValue("logFilekeepFiles"));
        if (logFileRoll) {
            List<File> depolyLogFileList = (List<File>) FileUtils.listFiles(new File("depoly"), new String[]{"out.log"}, true);
            for (int i = 0; i < depolyLogFileList.size(); i++) {
                File file = depolyLogFileList.get(i);
                if (file.length() > 1024 * 1024 * logFileRollSize) {
                    String oldFilePath = file.getAbsolutePath();
                    String newFilePath = file.getAbsolutePath() + CommonUtil.getDateTagToSecond();
                    String command = "cp " + oldFilePath + " " + newFilePath;
                    CMDUtil.executeLinuxCommand(command);
                    command = "cat /dev/null > " + oldFilePath + " ";
                    CMDUtil.executeLinuxCommand(command);
                }
            }
            List<ProjectDO> jarList = ProjectDB.selectList("DepolyType is null or DepolyType=''");
            for (ProjectDO entity : jarList) {
                String logPath = entity.getJarPackagePath() + File.separator + "deploylog/";
                File dir = new File(logPath);
                if (dir.exists()) {
                    File[] logFilelist = dir.listFiles(new FilenameFilter() {
                        @Override
                        public boolean accept(File dir, String name) {
                            if (name.contains(".out.log") && !name.equals(entity.getServiceName() + ".out.log")) {
                                return true;
                            } else {
                                return false;
                            }
                        }
                    });
                    List<File> logFileArrayList = new ArrayList<File>(Arrays.asList(logFilelist));
                    logFileArrayList.sort(new Comparator<File>() {
                        @Override
                        public int compare(File o1, File o2) {
                            Date date1 = ConvertUtil.convert2Date(FileUtil.getLastModifyTime(o1));
                            Date date2 = ConvertUtil.convert2Date(FileUtil.getLastModifyTime(o2));
                            return date2.compareTo(date1);
                        }
                    });
                    if (logFileArrayList.size() > logFilekeepFiles) {
                        for (int i = logFilekeepFiles - 1; i < logFileArrayList.size(); i++) {
                            logFileArrayList.get(i).delete();
                        }
                    }
                }
            }

            depolyLogFileList = (List<File>) FileUtils.listFiles(new File("depoly-war"), new String[]{"out.log"}, true);
            for (int i = 0; i < depolyLogFileList.size(); i++) {
                File file = depolyLogFileList.get(i);
                if (file.length() > 1024 * 1024 * logFileRollSize) {
                    String oldFilePath = file.getAbsolutePath();
                    String newFilePath = file.getAbsolutePath() + CommonUtil.getDateTagToSecond();
                    String command = "cp " + oldFilePath + " " + newFilePath;
                    CMDUtil.executeLinuxCommand(command);
                    command = "cat /dev/null > " + oldFilePath + " ";
                    CMDUtil.executeLinuxCommand(command);
                }
            }

            List<WarDO> warList = WarDB.selectList("DepolyType='war'");
            for (WarDO entity : warList) {
                String logPath = entity.getJarPackagePath() + File.separator + "deploylog/";
                File dir = new File(logPath);
                if (dir.exists()) {
                    File[] logFilelist = dir.listFiles(new FilenameFilter() {
                        @Override
                        public boolean accept(File dir, String name) {
                            if (name.contains(".out.log") && !name.equals(entity.getServiceName() + ".out.log")) {
                                return true;
                            } else {
                                return false;
                            }
                        }
                    });
                    List<File> logFileArrayList = new ArrayList<File>(Arrays.asList(logFilelist));
                    logFileArrayList.sort(new Comparator<File>() {
                        @Override
                        public int compare(File o1, File o2) {
                            Date date1 = ConvertUtil.convert2Date(FileUtil.getLastModifyTime(o1));
                            Date date2 = ConvertUtil.convert2Date(FileUtil.getLastModifyTime(o2));
                            return date2.compareTo(date1);
                        }
                    });
                    if (logFileArrayList.size() > logFilekeepFiles) {
                        for (int i = logFilekeepFiles - 1; i < logFileArrayList.size(); i++) {
                            logFileArrayList.get(i).delete();
                        }
                    }
                }
            }
        }
    }
}
